/*
 * CarlosLayerManager.java
 *
 * Created on 29 de Junho de 2007, 13:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Carlos;

import characters.BallCarlos;
import characters.Carlos;
import characters.CloudCarlos;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/**
 *
 * @author JAKE
 */
public class CarlosLayerManager extends LayerManager {
    //---------------------------------------------------------
    //   dimension fields
    //  (constant after initialization)
    
    /**
     * The x-coordinate of the place on the game canvas where
     * the LayerManager window should appear, in terms of the
     * coordiantes of the game canvas.
     */
    static int CANVAS_X;
    
    /**
     * The y-coordinate of the place on the game canvas where
     * the LayerManager window should appear, in terms of the
     * coordiantes of the game canvas.
     */
    static int CANVAS_Y;
    
    /**
     * The width of the display window.
     */
    static int DISP_WIDTH;
    
    /**
     * The height of this object's graphical region. This is
     * the same as the height of the visible part because
     * in this game the layer manager's visible part scrolls
     * only left and right but not up and down.
     */
    static int DISP_HEIGHT;
    
    //Ball carlos
    BallCarlos ballCarlos;
    
    //Charles
    Carlos carlos;
    
    //Cloud
    CloudCarlos cloud;
    
    /** Creates a new instance of CarlosLayerManager */
    public CarlosLayerManager(int x, int y, int width, int height) {
        CANVAS_X = x;
        CANVAS_Y = y;
        DISP_WIDTH = width;
        DISP_HEIGHT = height;
        setViewWindow(0, 0, DISP_WIDTH, DISP_HEIGHT);
    }
    
    public void execute() throws Exception{
        if(ballCarlos != null && carlos !=null) {
            if(ballCarlos.collidesWith(carlos, true)){
                ballCarlos.setGoingUp(!ballCarlos.isGoingUp());
                ballCarlos.setGoingRight(!ballCarlos.isGoingRight());
            }
            ballCarlos.execute();
            cloud.execute();
        }
    }
    
    public void checkKeys(int keyState){
        
        if(carlos != null){
            if ((keyState & GameCanvas.LEFT_PRESSED) != 0) {
                carlos.moveLeft();
            } else if ((keyState & GameCanvas.RIGHT_PRESSED) != 0) {
                carlos.moveRight();
            }
        }
    }
    
    public void paint(Graphics g) throws Exception {
        // create the player
        if(ballCarlos == null) {
            ballCarlos = new BallCarlos(DISP_WIDTH/1 - DISP_WIDTH/3,
                    DISP_HEIGHT - BallCarlos.getHEIGHT(), DISP_WIDTH, DISP_HEIGHT);
            append(ballCarlos);
        }
        if(carlos == null){
            carlos = new Carlos(DISP_WIDTH/3,
                    DISP_HEIGHT - 22, DISP_WIDTH, DISP_HEIGHT);
            append(carlos);
        }
        if(cloud==null){
            cloud = new CloudCarlos(DISP_WIDTH/6,DISP_HEIGHT/3,DISP_WIDTH,DISP_HEIGHT);
            append(cloud);
        }
        setViewWindow(CANVAS_X,CANVAS_Y,DISP_WIDTH,DISP_HEIGHT);
        paint(g,CANVAS_X,CANVAS_Y);
    }
    
    
}
